<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\User;

class UserController extends Controller {

    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index() {
        return Admin::content(function (Content $content) {

                    $content->header('用户');
                    $content->description('列表');

                    $content->body($this->grid());
                });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id) {
        return Admin::content(function (Content $content) use ($id) {

                    $content->header('用户');
                    $content->description('编辑');

                    $content->body($this->form()->edit($id));
                });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create() {
        return Admin::content(function (Content $content) {

                    $content->header('用户');
                    $content->description('创建');

                    $content->body($this->form());
                });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid() {
        return Admin::grid(User::class, function (Grid $grid) {

                    $grid->id('ID')->sortable();
                    $grid->username('用户名');
                    $grid->telphone('手机号码');
                    $grid->email('邮箱');
                    $grid->avatar('头像')->image(env('APP_URL'), 60, 60);
//                    $grid->status('状态')->display(function ($status) {
//                        return $status ? '正常' : '禁用';
//                    });
                    $states = [
                        'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                        'off' => ['value' => 0, 'text' => '禁用', 'color' => 'default'],
                    ];
                    $grid->status()->switch($states);
                    $grid->created_at('创建时间');
                    $grid->updated_at('更新时间');
                });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form() {
        return Admin::form(User::class, function (Form $form) {

                    $form->display('id', 'ID');
                    $form->image('avatar', '头像');
                    $form->text('username', '用户名')->rules('required|min:2');
                    $form->text('telphone', '手机号码')->rules(function ($form) {
                        // 如果不是编辑状态，则添加字段唯一验证
                        if (!$id = $form->model()->id) {
                            return 'unique:users,telphone';
                        } else {
                            return 'telphone';
                        }
                    });
                    $form->email('email', '邮箱');
                    $form->password('password', trans('admin.password'))->rules('required|confirmed');
                    $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
                            ->default(function ($form) {
                                return $form->model()->password;
                            });
                    $form->ignore(['password_confirmation']);
                    if ($form->model()->id) {
                        $form->switch('status', '状态')->states([
                                    'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                                    'off' => ['value' => 0, 'text' => '禁用', 'color' => 'default'],
                                ])
                                ->default(function ($form) {
                                    return $form->model()->status;
                                });
                    } else {
                        $form->switch('status', '状态')->states([
                                    'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                                    'off' => ['value' => 0, 'text' => '禁用', 'color' => 'default'],
                                ])
                                ->default(1);
                    }
                    $form->display('created_at', '创建时间');
                    $form->display('updated_at', '更新时间');
                    $form->saving(function (Form $form) {
                        if ($form->password && $form->model()->password != $form->password) {
                            $form->password = bcrypt($form->password);
                        }
                    });
                });
    }

}
